

import ij.*;
import ij.gui.*;

import java.awt.*;


import ij.plugin.filter.PlugInFilter;
import ij.process.*;


public class Cabochon_ implements PlugInFilter {

	public void run(ImageProcessor ip) {
//		 get width, height and the region of interest
		int w = ip.getWidth();     
		int h = ip.getHeight();    
		
		Rectangle roi = ip.getRoi();
		System.out.println(w*h+" "+w+" "+h+" "+roi.width); 

		// create a new image with the same size and copy the pixels of the original image
		ImagePlus inverted = NewImage.createRGBImage ("Inverted image", w, h, 1, NewImage.FILL_BLACK);
		ImageProcessor inv_ip = inverted.getProcessor();
		inv_ip.copyBits(ip,0,0,Blitter.COPY);
		int[] pixels = (int[]) inv_ip.getPixels();
		
		// invert the pixels in the ROI
		for (int i=roi.y; i<roi.y+roi.height; i++) {
			int offset =i*w;
			
			double idm=h/2.0*i+h/2; 
			int imoins=(int)idm; 
			for (int j=roi.x; j<roi.x+roi.width; j++) {
			
				double jdm=w/2.0*j+w/2.0; 
				int jmoins=(int)jdm; 
				int pos = offset+j;
				int cord=imoins*w+jmoins; 
				if ((cord<w*h)&&(cord>=0))
		    	pixels[pos] = ((int[]) inv_ip.getPixels())[cord];
				else
				pixels[pos]=0xFF00FF; 
			}
		}

		inverted.show();
		inverted.updateAndDraw();
	}

	
	public int setup(String arg, ImagePlus imp) {
		if (arg.equals("about"))
			{showAbout(); return DONE;}
	            return DOES_RGB+NO_CHANGES;

	}
	
	void showAbout() {
		IJ.showMessage("Cabochon",
			"Transform a rectangle into an ellipse"
		); 
}
}
